package com.webchat.ugc.service.chain;

import com.webchat.domain.vo.response.moment.MomentVO;

import java.util.ArrayList;
import java.util.List;

public class MomentPublishHandlerChain {

    /**
     * 管理动态后置处理任务责任链
     */
    public List<MomentPublishHandler> handlers = new ArrayList<>();

    private int execIndex = 0;

    public MomentPublishHandlerChain addHandler(MomentPublishHandler handler) {
        this.handlers.add(handler);
        return this;
    }

    public MomentPublishHandlerChain addHandler(List<MomentPublishHandler> handlers) {
        this.handlers.addAll(handlers);
        return this;
    }

    public void handle(MomentVO moment, MomentPublishHandlerChain chain) {

        if (execIndex == handlers.size()) {
            // 跳出责任链任务
            return;
        }
        MomentPublishHandler handler = handlers.get(execIndex++);
        handler.handle(moment, chain);
    }

}
